package com.hyper_yang.javase.supplement.net;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        System.out.println("=====Server2=====");
        // 创建服务端对象
        ServerSocket server = new ServerSocket(3000);
        // 绑定客户端 [阻塞状态]
        Socket client = server.accept();
        // 获取客户端字节流
        InputStream inputStream = client.getInputStream();
        // 读取数据
        byte[] bytes = new byte[1024];
        int len = -1;
        while ((len = inputStream.read(bytes)) != -1) {
            String s = new String(bytes, 0, len);
            System.out.println("客户端发送" + s);
        }
        // 基于客户端获取字节输出流  【要回应对应的客户端】
        OutputStream outputStream = client.getOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        printStream.print("嘿嘿嘿，这里是服务端，我收到消息了！！！😀");
        // 释放资源
        printStream.close();
        inputStream.close();
        client.close();
        server.close();
    }
}
